package com.sitechasia.webx.components.xtable.toolbar;

import java.util.Iterator;

import org.extremecomponents.table.bean.Export;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.TwoColumnRowLayout;
import org.extremecomponents.table.view.html.toolbar.ImageItem;
import org.extremecomponents.util.HtmlBuilder;

public class CustomPageToolbar extends TwoColumnRowLayout {
	public CustomPageToolbar(HtmlBuilder html, TableModel model) {
		super(html, model);
	}

	protected boolean showLayout(TableModel model) {
		boolean showStatusBar = BuilderUtils.showStatusBar(model);
		boolean filterable = BuilderUtils.filterable(model);
		boolean showExports = BuilderUtils.showExports(model);
		boolean showPagination = BuilderUtils.showPagination(model);
		boolean showTitle = BuilderUtils.showTitle(model);
		if (!showStatusBar && !filterable && !showExports && !showPagination
				&& !showTitle) {
			return false;
		}
		return true;
	}

	protected void columnLeft(HtmlBuilder html, TableModel model) {
		boolean showStatusBar = BuilderUtils.showStatusBar(model);
		if (!showStatusBar) {
			return;
		}
	}

	/**
	 * 扩展表头添加具体页数
	 */
	protected void columnRight(HtmlBuilder html, TableModel model) {
		boolean showPagination = BuilderUtils.showPagination(model);
		boolean showExports = BuilderUtils.showExports(model);
		CustomToolbarBuilder toolbarBuilder = new CustomToolbarBuilder(html,
				model);
		html.td(2).align("center").close();
		html.table(2).border("0").cellPadding("0").cellSpacing("0").styleClass(
				BuilderConstants.TOOLBAR_CSS).close();
		html.tr(3).close();
		if (showPagination) {
			html.td(4).close();
			toolbarBuilder.firstPageItemAsText();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.separatorLine();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.prevPageItemAsText();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.separatorLine();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.nextPageItemAsText();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.separatorLine();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.lastPageItemAsText();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.separatorLine();
			html.tdEnd();

			html.td(4).close();
			html.newline();
			toolbarBuilder.curTotalPage();

			html.tdEnd();

			html.td(4).close();
			html.newline();
			toolbarBuilder.separatorLine();
			html.tdEnd();

			html.td(4).close();
			toolbarBuilder.totalRows();
			html.tdEnd();

			html.td(4).close();
			html.append("转到 ");
			html.tdEnd();
			html.td(4).width("30").close();
			html.input("text").id("textNum").name("pages1").size("1").value("1").maxlength("3").style("width:100%")
					.xclose();
			html.tdEnd();
			html.td(4).close();
			html.append("页");
			html.tdEnd();
			html.td(4).close();

			ImageItem item7 = new ImageItem();
			item7.setImage(BuilderUtils.getImage(model, "go-1"));
			item7.setDisabledImage(BuilderUtils.getImage(model, "go-2"));
			item7.setStyle("border:none;vertical-align:middle;");
			toolbarBuilder.buildCurPage(html, model, item7);
			html.tdEnd();
			if (showExports) {
				html.td(4).close();
				toolbarBuilder.separatorLine();

				html.tdEnd();
			}
		}
		if (showExports) {
			Iterator iterator = model.getExportHandler().getExports()
					.iterator();
			for (Iterator iter = iterator; iter.hasNext();) {
				html.td(4).close();
				Export export = (Export) iter.next();
				toolbarBuilder.exportItemAsImage(export);
				html.tdEnd();
			}
		}
		html.trEnd(3);
		html.tableEnd(2);
		html.tdEnd();
	}
}